#include "widget_ir.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QProgressDialog>
#include <QTimer>
#include <QDateTime>

Widget_IR::Widget_IR(AudioDataProcess *adp,QWidget *parent)
    : QWidget(parent),audio_process(adp)
{
    layout_main = new QGridLayout;

    btn_open_ir = new QPushButton(tr("加载IR文件"));
    btn_open_src = new QPushButton(tr("加载声音文件"));
    btn_play_src = new QPushButton(tr("播放源声"));
    btn_play_res = new QPushButton(tr("播放IR处理后的声音"));
    lab_ir_path = new QLabel(tr(""));
    lab_src_path = new QLabel(tr(""));

    layout_main->addWidget(lab_src_path,0,0,1,1);
    layout_main->addWidget(btn_open_src,0,1,1,1);
    layout_main->addWidget(btn_play_src,0,2,1,1);
    layout_main->addWidget(lab_ir_path,1,0,1,1);
    layout_main->addWidget(btn_open_ir,1,1,1,1);
    layout_main->addWidget(btn_play_res,1,2,1,1);
    layout_main->setColumnStretch(0,80);
    layout_main->setColumnStretch(1,10);
    layout_main->setColumnStretch(2,10);

    connect(btn_open_ir,&QPushButton::clicked,this,&Widget_IR::btn_click_open_ir);
    connect(btn_open_src,&QPushButton::clicked,this,&Widget_IR::btn_click_open_src);
    connect(btn_play_src,&QPushButton::clicked,this,&Widget_IR::btn_click_play_src);
    connect(btn_play_res,&QPushButton::clicked,this,&Widget_IR::btn_click_play_res);
    connect(this,&Widget_IR::create_file,audio_process,&AudioDataProcess::create_file);

    // connect(this,&Widget_IR::open_audio_file,audio_process,&AudioDataProcess::open_file);

    setLayout(layout_main);
    resize(800,200);
    //QString path = "E:\\Matlab\\matlab\\IR\\IR_Files\\OH 412 MAR-CB M-PR25-B PO-Fat-10.wav";
    QString path = "F:/Music/ir/G12H Creambk 112 C TLM 107 RoomL Celestion.wav";
    lab_ir_path->setText(path);
    audio_process->set_ir_file_path(path);
    path = "F:/Music/clean.wav";
    lab_src_path->setText(path);
    audio_process->set_audio_file_path(path);

}

Widget_IR::~Widget_IR()
{

}
#include <QStandardPaths>
void Widget_IR::btn_click_open_src()
{
    QString path;

    path.clear();

    QStringList sl = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
    QString music_std_path;

    if(sl.size() > 0)
    {
        music_std_path = sl[0];
    }
    else
    {
        music_std_path = "";
    }

    path = QFileDialog::getOpenFileName(this, tr("打开"),music_std_path,tr("音频文件(*.wav *.wma)"));
    if(!path.isEmpty())
    {
        if(QFile(path).exists())
        {
            if(audio_process->set_audio_file_path(path) == AudioDataProcess::SUCCESS)
            {
                // setWindowTitle(path);
                lab_src_path->setText(path);
            }
            else
            {
                qDebug() << "不支持的音频格式";
                QMessageBox msgBox;
                msgBox.setText(tr("不支持的音频格式"));
                msgBox.setStandardButtons(QMessageBox::Ok);
                msgBox.setDefaultButton(QMessageBox::Ok);
                msgBox.setIcon(QMessageBox::Warning);
                msgBox.exec();
            }
        }
        else
        {

        }
    }
}

void Widget_IR::btn_click_open_ir()
{
    QString path;

    path.clear();

    QStringList sl = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
    QString music_std_path;

    if(sl.size() > 0)
    {
        music_std_path = sl[0];
    }
    else
    {
        music_std_path = "";
    }

    path = QFileDialog::getOpenFileName(this, tr("打开"),music_std_path,tr("音频文件(*.wav)"));
    if(!path.isEmpty())
    {
        if(QFile(path).exists())
        {
            if(audio_process->set_ir_file_path(path) == AudioDataProcess::SUCCESS)
            {
                // setWindowTitle(path);
                lab_ir_path->setText(path);
            }
            else
            {
                qDebug() << "不支持的音频格式";
                QMessageBox msgBox;
                msgBox.setText(tr("不支持的音频格式"));
                msgBox.setStandardButtons(QMessageBox::Ok);
                msgBox.setDefaultButton(QMessageBox::Ok);
                msgBox.setIcon(QMessageBox::Warning);
                msgBox.exec();
            }
        }
        else
        {

        }
    }
}

void Widget_IR::btn_click_play_src()
{

}

void Widget_IR::btn_click_play_res()
{
    qInfo() << "开始生成处理";
    QDateTime st = QDateTime::currentDateTime();
    emit create_file();
    QTimer *time = new QTimer;
    connect(time,&QTimer::timeout,this,&Widget_IR::compute_progress);
    time->start(50);

    QProgressDialog *dig_prog = new QProgressDialog(tr("正在计算..."),tr("取消"),0,100,this);
    connect(this,&Widget_IR::update_progress,dig_prog,&QProgressDialog::setValue);
    dig_prog->setWindowTitle(tr("数据处理"));
    dig_prog->exec();
    QDateTime et = QDateTime::currentDateTime();
    time->stop();
    time->deleteLater();
    delete time;
    dig_prog->deleteLater();
    delete dig_prog;
    qDebug() << "大约用时" << st.msecsTo(et) << "毫秒";
}

void Widget_IR::compute_progress()
{
    emit update_progress(audio_process->get_progress());
}
